const goodsService = require('../services/goodsService')

exports.getAllGoods = async (req,res) =>{
    try {
        const goods = await goodsService.findGoods(req.body);
        res.json(goods);        
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
};

exports.createGoods = async (req,res) =>{
    try {
        const newGoods = await goodsService.createGoods(req.body);
        res.status(201).json(newGoods);
    }catch(error) {
        res.status(400).json({error:error.message});
    }
};

exports.updateGoods = async (req,res) =>{
    try {
        const updatedGoods = await goodsService.updateaGoods(req.params.id,req.body);
        res.json(updatedGoods);
    } catch (error) {
        const statusCode = error.message === 'Job not found' ? 404 : 400;
        res.status(statusCode).json({error:error.message});
    }
};

exports.deleteGoods = async (req,res) =>{
    try {
        await goodsService.deleteGoods(req.params.id);
        res.status(204).send();
    } catch (error) {
        res.status(404).json({error: error.message});
    }
};

exports.filterGoods = async (req,res) => {
    try {
    const rows = await goodsService.filtrategoods(req.query || {});
        res.json(rows);
    } catch (err) {
        res.status(500).json({error:err.message});
    }
}
